package be.kdg.netpresentclient;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
* Special Panel used to show the Questions and answers and to ask new questions. 
*
* @autor Timothy Huber, Joris Clevenbergs, Rik verschuren, Wim Van der Wilt
*/
public class QnAPanel extends JPanel implements FocusListener, ActionListener {

	/**
	 * Default version ID
	 */
	private static final long serialVersionUID = 1L;

	private List<QuestionAskedEventListener> listeners = new ArrayList<QuestionAskedEventListener>();

	private Color color;
	private Dimension size;
	private JButton button;
	private JTextArea oldQuestions;
	private JTextArea newQuestion;
	private static String click = "klik hier voor uw vraag te stellen";
	private static String empty = "";
	private static int X = 820;
	private static int Y = 620;
	
	/**
	* Constructor
	* 
	* @param color	color of the panel.
	* @param size	dimension of the panel.
	*/
	public QnAPanel(Color color, Dimension size) {
		this.color = color;
		this.size = size;
		initialisePane(false); // this is for adding the old questions
		initialisePane(true); // this is for adding the new questions

		button = new JButton("Vraag stellen");
		button.setBounds(X, Y + 98, this.size.width, 20);
		button.setPreferredSize(new Dimension(this.size.width, 25));
		button.addActionListener(this);
		add(button);

	}
	
	/**
	* Updates the question field with a new text.
	* 
	* @param text	String containing the new text for the question field.
	*/
	public void updateText(String text) {
		// first we have to empty the text field before adding the new text
		oldQuestions.setText(null);
		oldQuestions.setText(text);
	}
	
	/**
	* Init function that initializes the textarea's 
	* 
	* @param editable	Boolean.
	*/
	private void initialisePane(boolean editable) {
		JTextArea questions = new JTextArea();
		Dimension newSize;
		questions.setLineWrap(true);
		questions.setWrapStyleWord(true);
		if (editable) {
			newQuestion = questions;
			newQuestion.setBounds(X, Y, size.width, 108);
			newSize = new Dimension(size.width, 105);
			questions.setBackground(Color.white);

			newQuestion.setText(click);
			newQuestion.addFocusListener(this);
		} else {
			oldQuestions = questions;
			newSize = new Dimension(size.width, 600);
			questions.setBackground(color);
		}
		questions.setEditable(editable);

		JScrollPane textScrollPane = new JScrollPane(questions);
		textScrollPane.setBorder(null);
		textScrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		textScrollPane
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		textScrollPane.setPreferredSize(newSize);

		add(textScrollPane);
	}

	/**
	* Overriding function that handles the FocusEvents when the question field gains focus.
	* 
	* @param e	FocusEvent that has to be handled.
	*/
	public void focusGained(FocusEvent e) {
		// The question field is emptied when it gains focus for the first time.
		if (newQuestion.getText().equals(click)) {
			newQuestion.setText(empty); 
		}
	}

	/**
	* Overriding function that handles the FocusEvents when the question field loses focus.
	* 
	* @param e	FocusEvent that has to be handled.
	*/
	public void focusLost(FocusEvent e) {
		// When the question field is empty we put the standard text back in.
		if (newQuestion.getText().equals(empty)) {
			newQuestion.setText(click);
		}
	}

	/**
	* Overriding function that handles the ActionEvents when the button is clicked.
	* 
	* @param e	ActionEvent that has to be handled.
	*/
	public void actionPerformed(ActionEvent e) {
		// If the button is clicked and a question is asked then we add the question. 
		if (!newQuestion.getText().equals(click)) {
			fireQuestionAskedEvent(newQuestion.getText());
			newQuestion.setText(click);
		}
	}

	/**
	* This function adds QuestionAskedEventListeners to the listeners list.
	* 
	* @param l	QuestionAskedEventListener that needs to be added to the listeners list.
	*/
	public synchronized void addListener(QuestionAskedEventListener l) {
		listeners.add(l);
	}

	/**
	* This function removes QuestionAskedEventListeners from the listeners list.
	* 
	* @param l	QuestionAskedEventListener that needs to be removed from the listeners list.
	*/
	public synchronized void removeListener(QuestionAskedEventListener l) {
		listeners.remove(l);
	}

	/**
	* A function that sends QuestionAskedEvents to all the listener when a question is asked.
	* 
	* @param question	String that contains the new question that has to be send to the listeners.
	*/
	private synchronized void fireQuestionAskedEvent(String question) {
		QuestionAskedEvent event = new QuestionAskedEvent(this, question);
		Iterator<QuestionAskedEventListener> itListeners = listeners.iterator();
		while (itListeners.hasNext()) {
			((QuestionAskedEventListener) itListeners.next())
					.QuestionAskedEventRecieved(event);
		}
	}
}